import { Logger } from "@/utils/Logger";

export class ChannelConfig {

    public static Config_Name: string = "channel"

    public static ChannelId = {
        BingNiao: 1, //冰鸟
        longwan: 2, //龙玩
        Dev: 3, //dev
        Test:4, //test
    }

    public id: number;
    public channename: string;

    public static configMap: Map<number, ChannelConfig> = new Map<number, ChannelConfig>();

    private static obj2Config(obj: object) {
        let config: ChannelConfig = new ChannelConfig();
        for (let key in obj) {
            if (typeof (obj[key]) !== "function")
                config[key] = obj[key];
        }
        return config;
    }

    public static json2Config(jsonObj: Object) {
        Object.keys(jsonObj).forEach((key) => {
            let config: ChannelConfig = this.obj2Config(jsonObj[key]);
            this.configMap.set(config.id, config);
        });
        Logger.log("ChannelConfig==", this.configMap)
    }

    public static getList(isExtend: boolean = true) {
        let resultList: Array<ChannelConfig> = [];
        this.configMap.forEach((value: ChannelConfig, key: number) => {
            resultList.push(value);
        });
        if (isExtend) {
            let channelConfig: ChannelConfig = new ChannelConfig()
            channelConfig.id = 0;
            channelConfig.channename = "请选择"
            resultList.unshift(channelConfig)
        }
        return resultList;
    }

    public static getChannelConfig(channelId: number, channelList: Array<ChannelConfig>=null) {
        channelList = channelList || this.getList();
        let channelConfig: ChannelConfig;
        for (let i = 0; i < channelList.length; i++) {
            if (channelList[i].id == channelId) {
                channelConfig = channelList[i];
                break;
            }
        }
        return channelConfig;
    }

    public static getChannelName(channelId: number, channelList: Array<ChannelConfig>=null) {
        let channelConfig: ChannelConfig = this.getChannelConfig(channelId, channelList);
        let channelName: string = ""
        if (channelConfig) {
            channelName = channelConfig.channename;
        }
        return channelName
    }
}